/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.dialog.newnode.view;

import java.net.URL;
import offset.nodes.client.dialog.newnode.model.ObjectType;
import offset.nodes.client.dialog.newnode.model.Processor;
import offset.nodes.client.dialog.newnode.model.SetupModel;
import offset.nodes.client.dialog.setup.view.SetupDialog;
import offset.nodes.client.dialog.newnode.model.NewNodeConfiguration;
import offset.nodes.client.view.ConfiguredApplet;

/**
 *
 * @author Walter Lütgenau
 */
public class NewNodeApplet extends ConfiguredApplet {

    SetupModel model = null;

    /** Creates a new instance of NewNodeApplet */
    public NewNodeApplet() {
    }

    public void initUI() {
        URL service = null;
        URL repository = null;
        try {
                service = new URL(getParameter("service"));
                repository = new URL(getParameter("base"));

        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        NewNodeConfiguration configuration = new NewNodeConfiguration(getParameter("path"), repository, service);
        model = configuration.getModel();
    }

    public void startUI() {
        // show dialog
        SetupDialog dialog = new SetupDialog(new javax.swing.JFrame(), true, model);
        dialog.setLocation(100, 100);
        dialog.setSize(980, 600);
        dialog.setVisible(true);
        int result = dialog.getReturnStatus();
        //dialog.dispose();

        // store result
        URL resultPath = null;
        if (result == SetupDialog.RET_OK) {
            ObjectType objectType = model.getObjectTypes()[model.getCurrentObjectType()];
            Processor processor = objectType.getProcessor();

            resultPath = processor.process();
        }

        // update page
        try {
            if (resultPath == null) {
                resultPath = new URL(getParameter("base") + "/" + getParameter("path"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(resultPath);
        getAppletContext().showDocument(resultPath);
    }
}
